package com.dataStatistics.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.dataStatistics.clinet.*;
import com.dataStatistics.entity.StatisticsDaily;
import com.dataStatistics.mapper.StatisticsDailyMapper;
import com.dataStatistics.service.IStatisticsDailyService;
import com.dataStatistics.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.parsing.QualifierEntry;
import org.springframework.stereotype.Service;

import java.lang.reflect.Array;
import java.util.*;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author Mr.Tang
 * @since 2022-06-07
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements IStatisticsDailyService {

    @Autowired
    private OrderClient orderClient;

    @Autowired
    private MemberClient memberClient;


    /**
     *
     * 每日更新统计数据
     *
     * */
    @Override
    public void updateStatisticsData(String day) {
        StatisticsDaily statisticsDaily = new StatisticsDaily();
        R buyCountToday = orderClient.getBuyCountToday(day);
        HashMap<String, Object> data = buyCountToday.getData();
        Map todayMap = (Map) data.get("todayMap");
        R registerCount = memberClient.getRegisterCount(day);
        HashMap<String, Object> registerCountData = registerCount.getData();
        Integer Rcount = (Integer)registerCountData.get("registerCount");
        Integer count = (Integer)todayMap.get("total");
        statisticsDaily.setRegisterNum(Rcount);
        statisticsDaily.setCourseNum(count);
        statisticsDaily.setLoginNum(30);
        statisticsDaily.setVideoViewNum(100);
        statisticsDaily.setDateCalculated(day);

        baseMapper.insert(statisticsDaily);



    }

    /**
     * 根据统计因子统计数据
     * */
    @Override
    public Map<String, Object> dataStatisticsByDivisor(String divisor, String start, String end) {
        QueryWrapper queryWrapper = new QueryWrapper();
        queryWrapper.between("date_calculated",start,end);

        List<StatisticsDaily> dataList = baseMapper.selectList(queryWrapper);

        //数据应该根据divisor进行筛选再传到前端
        ArrayList dateList = new ArrayList();
        ArrayList dataByDivisor = new ArrayList();

        for (StatisticsDaily s : dataList) {
                    dateList.add(s.getDateCalculated());

                    switch (divisor){
                        case "course_num":
                            dataByDivisor.add(s.getCourseNum());
                            break;
                        case "video_view_num":
                            dataByDivisor.add(s.getVideoViewNum());
                            break;
                        case "register_num":
                            dataByDivisor.add(s.getRegisterNum());
                            break;
                        default:
                            break;
                    }
        }
        Map<String ,Object> dataMap = new HashMap<>();
        dataMap.put("dataByDivisor",dataByDivisor);
        dataMap.put("dateList",dateList);
        return dataMap;
    }
}
